<?php

namespace App\Http\Controllers\H5;

use App\Models\InviteCode;
use App\Models\QrcodeString;
use App\Models\UserGroup;
use Barryvdh\Snappy\Facades\SnappyImage;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Str;

class InviteController extends BaseController
{
    public function index()
    {
        $group = UserGroup::userGroups()->orderBy('creditslower')->first();
        if ($this->user()->gid == $group->gid)
        {
            return $this->messager()->message('没有此权限')->tips('你当前的会员等级没有此项权限')->render();
        }

        $invite = InviteCode::find($this->uid);
        if (!$invite)
        {
            $code = strtoupper(Str::random(10));
            InviteCode::insert([
                'uid'=>$this->uid,
                'code'=>$code
            ]);
            $invite = InviteCode::find($this->uid);
        }

        $code = $invite->code;
        $qs = QrcodeString::where('uid', $this->uid)->where('type', 'invite')->first();
        if (!$qs){
            $qs = QrcodeString::create([
                'uid'=>$this->uid,
                'type'=>'invite'
            ]);
        }
        //$res = $this->officialAccount->qrcode->temporary($qs->id, 24 * 3600);
        $res = $this->officialAccount->qrcode->forever($qs->id);
        $qrcode = $this->officialAccount->qrcode->url($res['ticket']);
        //dd($res);
        $this->assign(compact('code', 'qrcode'));

        return $this->view('h5.invite.index');
    }

    public function image()
    {
        $invite = InviteCode::find($this->uid);
        if (!$invite)
        {
            $code = strtoupper(Str::random(10));
            InviteCode::insert([
                'uid'=>$this->uid,
                'code'=>$code
            ]);
            $invite = InviteCode::find($this->uid);
        }

        if (!$invite->image)
        {
            $code = $invite->code;
            $qs = QrcodeString::where('uid', $this->uid)->where('type', 'invite')->first();
            if (!$qs){
                $qs = QrcodeString::create([
                    'uid'=>$this->uid,
                    'type'=>'invite'
                ]);
            }
            //$res = $this->officialAccount->qrcode->temporary($qs->id, 24 * 3600);
            $res = $this->officialAccount->qrcode->forever($qs->id);
            $qrcode = $this->officialAccount->qrcode->url($res['ticket']);
            //dd($res);
            $this->assign(compact('code', 'qrcode'));

            $invite->image = 'invite/'.Str::random(40).'.png';
            SnappyImage::loadView('h5.invite.image', $this->data)->save(storage_public_path($invite->image));
            //InviteCode::where('uid', $this->uid)->update(['image'=>$invite->image]);
            $invite->save();
        }

        return response()->file(storage_public_path($invite->image));
    }

    public function bind(Request $request)
    {
        $upuid = intval($request->input('upuid'));
        if ($upuid && $upuid != $this->uid)
        {
            $relation = $this->user()->relation;
            if ($relation)
            {
                return $this->messager()->message('账号已经绑定')
                    ->tips('你已经绑定过其他的用户，不能重复绑定')
                    ->links([
                        ['继续', url('h5')]
                    ])->render();
            } else {
                $this->user()->relation()->create(['upuid'=>$upuid]);
            }

            return $this->messager()->message('账号绑定成功')
                ->tips('点击下面的按钮继续设置更多账号相关信息')
                ->links([
                    ['继续', url('h5/security')]
                ])->render();
        } else {
            return redirect('h5');
        }
    }


    /**
     * 获取分享相关信息
     *
     * @return multitype:string unknown
     */
    public function getShareContents()
    {
        $flag = request()->post('flag', 'shop');
        // 当前用户名称
        $share_url = 'http://www.zph298.com/h5/inviteshare/' . $this->uid.'.html';
        $current_user = "分享人：" . $this->username;
        $share_content['share_title'] = "您的好友邀请您加入证品荟";
        $share_content["share_url"] = $share_url;
        $share_content['share_content'] ="证品荟,一个只卖正品的商城!";
        $share_content["share_img"] = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1540637016663&di=7434e3044a26ccaed4a805f587357e2f&imgtype=0&src=http%3A%2F%2Fpic.58pic.com%2F58pic%2F15%2F57%2F05%2F258PICA58PICQiV_1024.png';
        $share_content['share_nick_name'] = $current_user;

        $app = $this->officialAccount;
        //$app->jssdk->setUrl($share_url);
       // $wxconfig =  $app->jssdk->buildConfig(
          //   array('onMenuShareAppMessage','onMenuShareQQ', 'onMenuShareWeibo'), true);
       // $share_content['wxconfig'] = $wxconfig;
        return $share_content;
    }

    /**
     * 用户点击分享的连接
     */
    public function inviteshare($uid)
    {
        $upload_path = "storage/qrcode"; //推广二维码手机端展示
        // 查询并生成二维码
        $path = $upload_path . '/qrcode_' . $uid . '.png';
        $app = $this->officialAccount;
        return $this->view('h5.invite.index', compact('path','app'));
    }

}
